package com.lz.controller.lg;

import com.alibaba.fastjson.JSONArray;
import com.lz.entity.Car;
import com.lz.entity.Customer;
import com.lz.entity.Employee;
import com.lz.entity.OrderSheet;
import com.lz.repository.lg.VO.OrderSheetVO;
import com.lz.service.lg.OrderSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.PageUitl;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderSheetController {
	@Autowired
	private OrderSheetService orderSheetService;

	//由其他点击事件跳转到添加订单的页面
	@RequestMapping("/addOrder")
	public String addOrder(Model model) {
		//得到最后按揭单的单号
		Integer integer = orderSheetService.selectMaxOrderId();
		model.addAttribute("maxId", integer + 1);
		return "lgaddOrder";
	}

	//保存订单
	@RequestMapping("/addOrderSheet")
	public String addOrderSheet(OrderSheetVO orderSheetVO, HttpServletRequest request) {
		Customer customer = new Customer();
		Car car = new Car();
		OrderSheet orderSheet = new OrderSheet();
		orderSheet.setOrderMoney(Integer.parseInt(orderSheetVO.getTotalPrice()));
		orderSheet.setOrderMsg(orderSheetVO.getOrderSheetMsg());
		orderSheet.setOrderSheetDate(new Date());
		orderSheet.setOrderState("已保存");
		customer.setCustomerName(orderSheetVO.getCustomerName());
		customer.setSex(orderSheetVO.getSex().charAt(0));
		customer.setCustomerCardId(orderSheetVO.getCustomerCardId());
		customer.setCustomerTel(orderSheetVO.getCustomerTel());
		customer.setCustomerAddress(orderSheetVO.getCustomerAddress());
		car.setBrand(orderSheetVO.getBrand());
		car.setSeries(orderSheetVO.getSeries());
		car.setModel(orderSheetVO.getModel());
		car.setConfigure(orderSheetVO.getConfigure());
		car.setColor(orderSheetVO.getColor());
		car.setPrice(Double.parseDouble(orderSheetVO.getPrice()));
		car.setVin(orderSheetVO.getVin());
		Employee employee = null;
		try {
			employee = (Employee) request.getSession().getAttribute("employee");
		} catch (Exception e) {
			e.printStackTrace();
		}
		orderSheetService.addOrderSheet(orderSheet, customer, car, employee);
		return "redirect:/selectOrderSheet";
	}


	//跳转到查询订单页面及模糊查询功能
	@RequestMapping("/selectOrderSheet")
	public String selectOrderSheet(Model model, String pageNo, String customerName, String brand) {
		if (pageNo == null) {
			pageNo = "1";
		}
		int pageSize = 3;
		Integer rows = orderSheetService.rowsOfOrderSheet(null, null);
		PageUitl<Object> pageUitl = new PageUitl<>(Integer.parseInt(pageNo), pageSize, rows);
		Integer pageTotal = pageUitl.getPageTotal();
		List<OrderSheetVO> list = orderSheetService.selectOrderSheet(customerName, pageUitl.getPageNo(), pageSize, brand);
		model.addAttribute("listlg", list);
		model.addAttribute("pageTotal", pageTotal);
		model.addAttribute("pageNo", pageUitl.getPageNo());
		return "lgselectOrder";
	}

	//条件查询及分页
	@RequestMapping(value = "/nextPage", produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public String nextPage(Integer pageNo, String customerName, String brand) {
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = 1;
		}
		int pageSize = 3;
		Integer rows = orderSheetService.rowsOfOrderSheet(customerName, brand);
		PageUitl<Object> pageUitl = new PageUitl<>(pageNo, pageSize, rows);
		Integer pageTotal = pageUitl.getPageTotal();
		List<OrderSheetVO> list = orderSheetService.selectOrderSheet(customerName, pageUitl.getPageNo(), pageSize, brand);
		HashMap<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("pageTotal", pageTotal);
		map.put("pageNo", pageNo);
		return JSONArray.toJSONString(map);
	}

	//查看详情
	@RequestMapping("/orderDetails")
	public String orderDetails(Integer orderSheetId, Model model) {
		//根据主键查询到对应的信息
		//根据订单id查询到相应的订单
		Map<String, Object> map = orderSheetService.findByorderSheetId(orderSheetId);
		OrderSheet orderSheet = (OrderSheet) map.get("orderSheet");
		//空值修改按钮是否隐藏
		if(orderSheet.getOrderState().equals("已提交")){
			model.addAttribute("hidden","hidden");
		}else{
			model.addAttribute("hidden","no");
		}
		Car car = (Car) map.get("car");
		Customer customer = (Customer) map.get("customer");
		OrderSheetVO orderSheetVO = new OrderSheetVO();
		orderSheetVO.setOrderSheetId(String.valueOf(orderSheet.getOrderId()));
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String format = simpleDateFormat.format(orderSheet.getOrderSheetDate());
		orderSheetVO.setMinopeningTime(format);
		orderSheetVO.setTotalPrice(String.valueOf(orderSheet.getOrderMoney()));
		orderSheetVO.setOrderState(orderSheet.getOrderState());
		orderSheetVO.setCustomerName(customer.getCustomerName());
		orderSheetVO.setSex(String.valueOf(customer.getSex()));
		orderSheetVO.setCustomerCardId(customer.getCustomerCardId());
		orderSheetVO.setCustomerTel(customer.getCustomerTel());
		orderSheetVO.setCustomerAddress(customer.getCustomerAddress());
		orderSheetVO.setBrand(car.getBrand());
		orderSheetVO.setSeries(car.getSeries());
		orderSheetVO.setModel(car.getModel());
		orderSheetVO.setConfigure(car.getSeries());
		orderSheetVO.setColor(car.getColor());
		orderSheetVO.setPrice(String.valueOf(car.getPrice()));
		orderSheetVO.setVin(car.getVin());
		model.addAttribute("orderSheetVO", orderSheetVO);
		return "orderDetails";
	}

	//封装map条件查询
	@RequestMapping("/selectByMap")
	public String selectByMap(String customerName, String brand, String pageNo) {
		Map map = new HashMap();
		map.put("customer.customerName", customerName);
		map.put("car.brand", brand);
		int pageSize = 3;
		Integer rows = orderSheetService.rowsByMap(map);
		System.out.println(rows);
		return "orderDetails";
	}

	//修改订单信息
	@RequestMapping("/changeOrderSheet")
	public String changeOrderSheet(OrderSheetVO orderSheetVO, Model model) {
		orderSheetService.changeOrderSheet(orderSheetVO);
		return "redirect:/selectOrderSheet";
	}

	//删除订单
	@RequestMapping("/deleteOrderSheet")
	public String deleteOrderSheet(Integer orderSheetId) {
		orderSheetService.deleteOrderSheet(orderSheetId);
		return "redirect:/selectOrderSheet";
	}

	//提交订单
	@RequestMapping("/submitOrderSheet")
	public String submitOrderSheet(Integer orderSheetId) {
		orderSheetService.submitOrderSheet(orderSheetId);
		return "redirect:/selectOrderSheet";
	}
}
